package edu.nyu.hps.architecture;
/**
 * This class is created as a supplementary class for defining the moves of cat
 * @author hiralrajani
 *
 */
public class CatMoves {

	int x, y, oppX, oppY;
	int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;
	int UP_RIGHT = 10, UP_LEFT = 11, DOWN_RIGHT = 12, DOWN_LEFT = 13;
	int[][] locations = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 },
			{ 0, 0 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };

	public CatMoves(int x, int y, int oppX, int oppY) {
		this.x = x;
		this.y = y;
		this.oppX = oppX;
		this.oppY = oppY;
	}

	private double distance(int x1, int y1, int x2, int y2) {
		return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	}

	/**
	 * Modified this method which returns the cat moves. It goes towards the
	 * mouse as much as possible If it is on the same square as mouse, cat
	 * stops.
	 * 
	 * @return The move string for the cat
	 */
	public String getMove() {
//		long time = System.currentTimeMillis();
//		while(System.currentTimeMillis() - time < 1000){
//			
//		}
		String move = "MOVE";
		double d1, d2;
		for (int a = 0; a < 4; a++) {
			int mx = 0, my = 0;

			for (int i = -1; i <= 1; i++) {
				for (int j = -1; j <= 1; j++) {
					d1 = distance(x + i, y + j, oppX, oppY);
					d2 = distance(x + mx, y + my, oppX, oppY);
					if (d1 < d2 && isInBoundary(x+i, y+j)) {
						mx = i;
						my = j;
					}
				}
			}
			x += mx;
			y += my;
			move += ":" + x + "," + y;
			if (x == oppX && y == oppY)
				break;
		}
		return move;
	}

	public boolean isInBoundary(int x, int y) {
		if (x < 0 || y < 0 || x > 100 || y > 100) {
			return false;
		}
		if ((x >= 50 && x <= 65) && (y >= 50 && y <= 65)) {
			return false;
		}
		return true;
	}

}
